package vsm.vsm_server.service.impl;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.*;
import org.springframework.stereotype.Service;
import vsm.vsm_server.pojo.Video.VideoUrlVO;
import vsm.vsm_server.service.VodService;

import java.util.*;

import static vsm.vsm_server.component.oss.InitAcsClint.initVodClient;

@Service
public class VodServiceImpl implements VodService {
    String accessKeyId ="LTAI5tEdXUnEBu5V7sQVnkJs";
    String accessKeySecret = "PRYEoatScwy8LlRROPbaRJ0DYxtdr9";
    @Override
    public String getAuth(String videoId) {
        try {
            DefaultAcsClient client = initVodClient(accessKeyId,accessKeySecret);
            //获取视频凭证的request和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
            //向request设置videoId
            request.setVideoId(videoId);
            //调用初始化对象方法得到凭证
            try {
                response=client.getAcsResponse(request);
            } catch (ClientException e) {
                e.printStackTrace();
            }
            return response.getPlayAuth();

        } catch (ClientException e) {
            return null;
        }

    }

    @Override
    public VideoUrlVO getUrl(String videoId) {
        try {
            DefaultAcsClient client = initVodClient(accessKeyId,accessKeySecret);
            //获取视频凭证的request和response对象
            GetPlayInfoRequest request = new GetPlayInfoRequest();
            GetPlayInfoResponse response = new GetPlayInfoResponse();
            //向request设置videoId
            request.setVideoId(videoId);
            //调用初始化对象方法得到凭证
            try {
                response=client.getAcsResponse(request);
            } catch (ClientException e) {
                e.printStackTrace();
            }
            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
            if (playInfoList.stream().count() > 0)
            {
                VideoUrlVO v = new VideoUrlVO();
                v.setCoverUrl(response.getVideoBase().getCoverURL());
                v.setVideoUrl(playInfoList.getFirst().getPlayURL());
                return v;
            }
            return null;

        } catch (ClientException e) {
            return null;
        }
    }

    @Override
    public String getPicture(String pictureId) {
        try {
            DefaultAcsClient client = initVodClient(accessKeyId,accessKeySecret);
            //获取视频凭证的request和response对象
            GetImageInfoRequest request = new  GetImageInfoRequest();
            GetImageInfoResponse response = new GetImageInfoResponse();
            request.setImageId(pictureId);
            response=client.getAcsResponse(request);
            GetImageInfoResponse.ImageInfo imageInfoList = response.getImageInfo();
             return imageInfoList.getURL();
        }catch (ClientException e) {
            return null;
        }
    }
    @Override
    public Map<String,String> getPictureList(List<String> pictureIdList){
        try {
            DefaultAcsClient client = initVodClient(accessKeyId,accessKeySecret);
            GetImageInfosRequest request = new GetImageInfosRequest();
            //转成set去重
            Set<String> pictureSet = new HashSet<>(pictureIdList);
            List<String> uniquePictureIds = new ArrayList<>(pictureSet);

            int batchSize = 20; // 每批处理的数量
            int totalIds = uniquePictureIds.size();

            Map<String,String> pictureMap = new HashMap<>();
            for(int i = 0; i < totalIds; i += batchSize){
                // 2. 获取当前批次的ID列表
                int end = Math.min(i + batchSize, totalIds);
                List<String> currentBatchIds = uniquePictureIds.subList(i, end);
                if (currentBatchIds.isEmpty()) {
                    continue; // 如果子列表为空（理论上不应该在 i < totalIds 时发生）
                }

                // 3. 将当前批次的ID合并成逗号分隔的字符串
                String imageIdsString = String.join(",", currentBatchIds);
                request.setImageIds(imageIdsString);
                // 4. 发送请求并获取响应
                GetImageInfosResponse response =client.getAcsResponse(request);

                // 5. 处理响应，将结果放入Map
                if (response != null && response.getImageInfo() != null) {
                    List<GetImageInfosResponse.Image> imageInfoList = response.getImageInfo();
                    for (GetImageInfosResponse.Image imageInfo : imageInfoList) {
                        if (imageInfo != null && imageInfo.getImageId() != null && imageInfo.getURL() != null) {
                            pictureMap.put(imageInfo.getImageId(), imageInfo.getURL());
                        }
                    }
                }
            }
            return pictureMap;

        } catch (ClientException e) {
            return null;
        }
    }
}
